<script setup lang="tsx">
import { ElButton, ElPopconfirm, ElTag } from 'element-plus';
import { fetchGetDeptList } from '@/service/api';
import { $t } from '@/locales';
import { deptLevelCodeRecord } from '@/constants/business';
import { useTable, useTableOperate } from '@/hooks/common/table';
import DeptOperateDrawer from './modules/dept-operate-drawer.vue';
import DeptSearch from './modules/dept-search.vue';

defineOptions({ name: 'DeptManage' });

const {
  columns,
  columnChecks,
  data,
  getData,
  getDataByPage,
  loading,
  mobilePagination,
  searchParams,
  resetSearchParams
} = useTable({
  apiFn: fetchGetDeptList,
  showTotal: true,
  apiParams: {
    current: 1,
    size: 10,
    levelCode: undefined,
    sortNum: undefined
    // userGender: undefined,
    // directorName: undefined,
    // phone: undefined,
    // email: undefined
  },
  columns: () => [
    { type: 'selection', width: 48 },
    // { prop: 'index', label: $t('common.index'), width: 64 },
    { prop: 'name', label: $t('page.manage.dept.name'), minWidth: 100 },
    // {
    //   prop: 'userGender',
    //   label: $t('page.manage.user.userGender'),
    //   width: 100,
    //   formatter: row => {
    //     if (row.userGender === undefined) {
    //       return '';
    //     }

    //     const tagMap: Record<Api.SystemManage.UserGender, UI.ThemeColor> = {
    //       1: 'primary',
    //       2: 'danger'
    //     };

    //     const label = $t(userGenderRecord[row.userGender]);

    //     return <ElTag type={tagMap[row.userGender]}>{label}</ElTag>;
    //   }
    // },
    {
      prop: 'levelCode',
      label: $t('page.manage.dept.levelCodeTitle'),
      minWidth: 100,
      formatter: row => {
        const label = $t(deptLevelCodeRecord[row.levelCode]);
        return <ElTag type="info">{label}</ElTag>;
      }
    },
    {
      prop: 'directorName',
      label: $t('page.manage.dept.directorName'),
      align: 'left',
      minWidth: 300
    },
    { prop: 'sortNum', label: $t('page.manage.dept.sortNum'), width: 120 },
    {
      prop: 'operate',
      label: $t('common.operate'),
      align: 'center',
      formatter: row => (
        <div class="flex-center">
          <ElButton type="primary" plain size="small" onClick={() => edit(row.id)}>
            {$t('common.edit')}
          </ElButton>
          <ElPopconfirm title={$t('common.confirmDelete')} onConfirm={() => handleDelete(row.id)}>
            {{
              reference: () => (
                <ElButton type="danger" plain size="small">
                  {$t('common.delete')}
                </ElButton>
              )
            }}
          </ElPopconfirm>
        </div>
      )
    }
  ]
});

const {
  drawerVisible,
  operateType,
  editingData,
  handleAdd,
  handleEdit,
  checkedRowKeys,
  onBatchDeleted,
  onDeleted
  // closeDrawer
} = useTableOperate(data, getData);

async function handleBatchDelete() {
  // eslint-disable-next-line no-console
  console.log(checkedRowKeys.value);
  // request

  onBatchDeleted();
}

function handleDelete(id: string) {
  // eslint-disable-next-line no-console
  console.log(id);
  // request

  onDeleted();
}

function edit(id: string) {
  handleEdit(id);
}
</script>

<template>
  <div class="min-h-500px flex-col-stretch gap-16px overflow-hidden lt-sm:overflow-auto">
    <DeptSearch v-model:model="searchParams" @reset="resetSearchParams" @search="getDataByPage" />
    <ElCard class="sm:flex-1-hidden card-wrapper" body-class="ht50">
      <template #header>
        <div class="flex items-center justify-between">
          <p>{{ $t('page.manage.user.title') }}</p>
          <TableHeaderOperation
            v-model:columns="columnChecks"
            :disabled-delete="checkedRowKeys.length === 0"
            :loading="loading"
            @add="handleAdd"
            @delete="handleBatchDelete"
            @refresh="getData"
          />
        </div>
      </template>
      <div class="h-[calc(100%-50px)]">
        <ElTable
          v-loading="loading"
          height="100%"
          border
          class="sm:h-full"
          :data="data"
          row-key="id"
          @selection-change="checkedRowKeys = $event"
        >
          <ElTableColumn v-for="col in columns" :key="col.prop" v-bind="col" />
        </ElTable>
      </div>
      <div class="mt-20px flex justify-end">
        <ElPagination
          v-if="mobilePagination.total"
          layout="total,prev,pager,next,sizes"
          v-bind="mobilePagination"
          @current-change="mobilePagination['current-change']"
          @size-change="mobilePagination['size-change']"
        />
      </div>

      <DeptOperateDrawer
        v-model:visible="drawerVisible"
        :operate-type="operateType"
        :row-data="editingData"
        @submitted="getDataByPage"
      />
    </ElCard>
  </div>
</template>

<style lang="scss" scoped>
:deep(.el-card) {
  .ht50 {
    height: calc(100% - 50px);
  }
}
</style>
